﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Portal.Environment;
using Portal.Interface;
using Portal.Language;

using System.Text;

public partial class WebPartConfigForm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Environments.SetEnvironment();
        Session["ActiveTheme"] = Environments.GetAppSettings("ActiveTheme");
        LoadWebPart();
        LoadCurrentUserInfo();
    }

    private void LoadCurrentUserInfo()
    {
        string Display = "guest";
        if (Environments.CurrentLoginUser != null)
        {
            Display = Environments.CurrentLoginUser.LoginName + " / " + Environments.CurrentLoginUser.FullName;
        }
    }


    private void LoadWebPart()
    {
        try
        {
            if (Environments.CheckSessionTimeOut(false))
            {
                placeHolderContent.Controls.Add(new System.Web.UI.LiteralControl(ResourceBLL.GetText("Home", "SessionTimeOut", "Session timeout! Xin vui lòng đăng nhập lại") + "&nbsp;<input type=button onclick=javascript:window.close(); value=\"" + ResourceBLL.GetText("Home","CloseText","Đóng") + "\">"));
                return;
            }
            string WebPartId = Request["WebPartId"];
            string RecordId = Request["RecordId"];
            if (WebPartId == null)
            {
                placeHolderContent.Controls.Add(new System.Web.UI.LiteralControl(ResourceBLL.GetText("Home","WebPartNotFound","Không tìm thấy webpart {WebPartId}")));
                return;
            }
            WebPartInt
                LoadedWebPartInt = WebPartUtilities.FindWebPart(WebPartId);
            if (LoadedWebPartInt == null)
            {
                string Msg =  ResourceBLL.GetText("Home","WebPartNotFound","Không tìm thấy webpart {WebPartId}");
                Msg = Msg.Replace("{WebPartId}", WebPartId);
                placeHolderContent.Controls.Add(new System.Web.UI.LiteralControl(Environments.GetFormDialog( "<div class=cssWebPartNotFound>" + Msg + "</div>","Config")));
                return;
            }

            if (LoadedWebPartInt.WebConfig == null)
            {
                string Msg = ResourceBLL.GetText("Home", "ConfigNotFound", "Không cung cấp cấu hình cho webpart {WebPartId}");
                Msg = Msg.Replace("{WebPartId}", WebPartId);
                placeHolderContent.Controls.Add(new System.Web.UI.LiteralControl(Environments.GetFormDialog("<div class=cssWebPartNotFound>" + Msg + "</div>","Config")));
                return;
            }
            IWebPartConfig
                FoundWebPartConfig = LoadedWebPartInt.WebConfig;
            FoundWebPartConfig.AjaxProReg(this);
            ConfigParamCls ConfigParamCls = new ConfigParamCls(WebPartId, RecordId);
            placeHolderContent.Controls.Add(new System.Web.UI.LiteralControl(FoundWebPartConfig.RenderWebPart(ConfigParamCls)));
        }
        catch (Exception ex)
        {
            placeHolderContent.Controls.Add(new System.Web.UI.LiteralControl(ex.Message.ToString()));
        }
    }
}
